Skip to content

ref(nextjs): Simplify adding default integrations when initializing SDK #5702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

lobsterkatie
Copy link
Member

@lobsterkatie lobsterkatie commented Sep 7, 2022

While working on adding a RequestData integration to the nextjs SDK, I found that the functions we use to set default integrations in the SDK were a little hard to parse, primarily because of a docstring that didn't make any sense and vague and/or slightly inaccurate function and variable names. We also have been making things more complicated than necessary, in that

a) The existing functions in utils/userIntegrations.ts use the given default integration instance if no user instance is found. Meanwhile, the two index files do the same in the case that no user integrations are provided at all. But those two conditions aren't logically independent, so by simply providing a default (empty) value for the second case, we can rely on the first check to ensure the default instance is used. (See the now-removed if (options.integrations) check in both of the index files.)

b) The options we want to force onto any user instance we find have been living in a nested object which really doesn’t need to be nested. (See the Options - now ForcedIntegrationOptions - type in utils/userIntegrations.ts.)

c) We've essentially been manually implementing Array.find, which there's no reason for us to do. (See the for-loop in addIntegrationToArray - now addOrUpdateIntegrationInArray - in utils/userIntegrations.ts.)

This fixes those problems, rewrites the docstring, and renames a bunch of variables. It also

  • Reorganizes the tests testing the adding of default integrations and refactors them to stop depending on the order or size of the eventual integrations array when testing an individual integration type.
  • Fixes a small regex bug in setNestedKey.

For ease of reviewing, the docstring/renaming changes have been separated into a separate commit from the logic changes, test reorg, and bugfix.

@github-actions
Copy link
Contributor

github-actions bot commented Sep 7, 2022

size-limit report 📦

Path Size
@sentry/browser - ES5 CDN Bundle (gzipped + minified) 19.46 KB (0%)
@sentry/browser - ES5 CDN Bundle (minified) 60.11 KB (0%)
@sentry/browser - ES6 CDN Bundle (gzipped + minified) 18.04 KB (0%)
@sentry/browser - ES6 CDN Bundle (minified) 53.03 KB (0%)
@sentry/browser - Webpack (gzipped + minified) 19.83 KB (0%)
@sentry/browser - Webpack (minified) 64.42 KB (0%)
@sentry/react - Webpack (gzipped + minified) 19.85 KB (0%)
@sentry/nextjs Client - Webpack (gzipped + minified) 44.84 KB (-0.07% 🔽)
@sentry/browser + @sentry/tracing - ES5 CDN Bundle (gzipped + minified) 25.97 KB (0%)
@sentry/browser + @sentry/tracing - ES6 CDN Bundle (gzipped + minified) 24.35 KB (0%)

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-simplify-adding-default-integrations branch from 0bd5df0 to 5b65fff Compare September 7, 2022 07:48
return [defaultBrowserTracingIntegration];
}
return addOrUpdateIntegration(defaultBrowserTracingIntegration, userIntegrations, {
'options.routingInstrumentation': nextRouterInstrumentation,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this API, couldn't we collide if integrations have the same option name?

Copy link
Member Author

@lobsterkatie lobsterkatie Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the same API as before, just a slightly simpler implementation - it still filters on integration name. See the const userInstance = userIntegrations.find(integration => integration.name === defaultIntegrationInstance.name); line in addOrUpdateIntegrationInArray.

What it doesn't protect against is duplicates (the way the main integration logic does), but hopefully users won't be giving us two copies of the same integration.

@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-simplify-adding-default-integrations branch 2 times, most recently from 3fb85ca to 4a9c167 Compare September 8, 2022 05:37
@lobsterkatie lobsterkatie force-pushed the kmclb-nextjs-simplify-adding-default-integrations branch from 4a9c167 to 0414730 Compare September 8, 2022 06:13
@lobsterkatie lobsterkatie merged commit 50c84c6 into master Sep 9, 2022
@lobsterkatie lobsterkatie deleted the kmclb-nextjs-simplify-adding-default-integrations branch September 9, 2022 14:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants